<?php if ( ! defined('ABSPATH')) exit('restricted access');

/*
	Plugin Name: WPnukes Symptom Checker
	Plugin URI: http://www.wpnukes.com/plugins/symptom-checker/
	Description: A wpnukes symptom checker plugin
	Version: 1.0
	Author: WPnukes
	Author URI: http://www.wpnukes.com/
*/

class wpnukes_symptom_checker
{
	private $baseurl = '', $path = '', $data = array(), $slug = 'symptom_checker', $text_domain = 'wpnukes_symptom_checker';
	protected $_webnukes = array();
	
	function __construct()
	{
		/** Plugin Base URL */
		$this->baseurl = plugin_dir_url( __FILE__ );

		/** Plugin Path */
		$this->path = plugin_dir_path( __FILE__ );
		
		/** Admin menu */
		add_action('admin_menu', array($this, 'admin_menu'));
		
		if(isset($_GET['page']) && $_GET['page'] == $this->slug)
		{
			add_action( 'admin_print_scripts', array($this, 'admin_script'));
			add_action( 'admin_print_styles', array($this, 'admin_style'));
		}
		
		/** Ajax Callback */
		add_action('wp_ajax_symptom_checker', array( $this, 'update_symptoms'));
		
		$this->gender = array('man'=>__('Man'), 'woman'=>__('Woman'), 'kid'=>__('Kid'));
		ini_set('max_input_vars', 3000);
	}
	
	function admin_script()
	{
		if(isset($_GET['tab']) && $_GET['tab'] == 'manage_settings')
			wp_enqueue_script( 'symptom-checker-function', $this->baseurl . 'js/backend.js', array('jquery-ui-sortable'));
	}
	
	function admin_style()
	{
		if(isset($_GET['tab']) && $_GET['tab'] == 'manage_settings')
			wp_enqueue_style( 'symptom-checker-style', $this->baseurl . 'css/style.css');
	}
	
	function symptom_checker()
	{	
		
		$this->_webnukes = &$GLOBALS['_webnukes']; /** Clone the fw_base_class object */
		$this->_webnukes->load('html_class', '', true); /** Load WPnukes HTML class */
		$this->_webnukes->load('wp_html_class','html'); /** Load wordpress HTML class */
		
		/** The tabs to create on symptom checker page */
		$tabs = array( 'general_settings'=>'General Settings', 'manage_settings'=>'Manage Symptoms');
		
		/** Select the default tab; Security the method must have _ before it's name*/
		$current = (isset($_GET['tab']) && method_exists( __CLASS__, '_'.$_GET['tab'])) ? $_GET['tab'] : 'general_settings';
	
		/** Create the Tabs HTML */
		$data = array('tabs'=>$tabs, 'page'=>$_GET['page'], 'current'=>$current);
		$this->data['tabs'] = $this->_webnukes->html->layout('wp_admin/tabs', $data, true);

		/** Function to handle tab */
		$method = '_'.$current;
		$this->$method();
		
		echo $this->_webnukes->html->build();
	}
	
	function _general_settings()
	{
		//TODO:load the files through config class so we can check the missing settings keys
		
		//Load default settings
		if(file_exists($this->path . 'config/symptom_checker.php'))
			require_once($this->path . 'config/symptom_checker.php');
			
		if( ! empty($_POST))
		{
			/** Verify the nonce */
			if( ! wp_verify_nonce( $_POST['symptom_checker_gs'], 'general_settings') )
				wp_die( __('Sorry, your nonce did not verify.', THEME_NAME) );
				
			/** Validation */
			/** Load validation class */
			$this->_webnukes->load('validation_class');
			
			foreach($options as $field=>$settings)
				$this->_webnukes->validation->set_rules($field, '<strong>'.$settings['label'].'</strong>', $settings['validation']);
			
			if($this->_webnukes->validation->run() !== false)
			{
				$data = array();
				
				foreach($options as $k=>$v)
					$data[$k] = $this->_webnukes->validation->post($k);
				
				update_option(THEME_PREFIX.$this->slug, $data);
				$this->_webnukes->msg->create( __('Settings Saved!', THEME_NAME), 'updated' );
			}
		}
		
		/** Store or load the default data */
		if( ! $settings = get_option(THEME_PREFIX.$this->slug))
			add_option(THEME_PREFIX.$this->slug, $this->_webnukes->config->read_settings($options), '', 'no');

		$data = array();
		foreach($options as $k=>$v)
		{
			$value = (isset($settings[$k])) ? $settings : array($k=>$v['std']);
			$data['fields'][$k] = $this->_webnukes->html->generator($k, $v, $value, 'array');
		}

		$content = $this->data['tabs'].$this->_webnukes->html->layout('wp_admin/form_table', $data, true);
		$this->_webnukes->html->layout('wp_admin/wrap', array('action'=>'admin.php?page='.$this->slug,'content'=>$content,'nonce_action'=>'general_settings','nonce_name'=>'symptom_checker_gs'));
		
		/** Clear the data variable to reduce the load on memory */
		$this->data = array();
	}
	
	function _manage_settings()
	{
		$settings = array();
		$dbkey = 'wpnukes_'.$this->slug.'_';
		foreach($this->gender as $gender=>$value)
		{
			if($data = get_option($dbkey.$gender)) $settings[$gender] = $data;
			elseif(file_exists($this->path.'temp/'.$gender.'.php')) include_once($this->path.'temp/'.$gender.'.php');
		}

		$this->data['symptoms'] = $settings;
		$this->_webnukes->html->layout($this->path . 'views/manage_settings', $this->data);
	}
	
	function admin_menu()
	{
		/** Check if the current user can view doctor informaiton */
		add_menu_page( __('Symptom Checkers'), __('Symptoms'), 'manage_options', 'symptom_checker', array($this, 'symptom_checker'), $this->baseurl.'images/symptom-icon.png');
	}

	function update_symptoms()
	{
		
		$symptoms = json_decode(stripslashes($_POST['symptoms']), true);
		if( ! is_array($symptoms) || empty($symptoms)) die('<div class="error"><p>Invalid request</p></div>');

		$dbkey = 'wpnukes_'.$this->slug.'_';
		foreach($this->gender as $gender=>$value)
		{
			$data = (isset($symptoms[$gender])) ? $symptoms[$gender] : array();
			update_option($dbkey.$gender, $data);
		}
		
		die('<div class="updated"><p>The settings saved successfully.</p></div>'); // this is required to return a proper result
	}
}

/** Initiate the Class */
$wpnukes = new wpnukes_symptom_checker;